import { add_edit_bioland } from '@/services/devices';
import { get_patient_select } from '@/services/patient';
import { ActionType, ProForm, ProFormSelect, ProFormText } from '@ant-design/pro-components';
import { Divider, Modal, message } from 'antd'
import React from 'react'

interface Iprops {
    isShowAddModal: (show: boolean) => void;
    addModal: boolean;
    actionRef: React.MutableRefObject<ActionType | undefined>;
    // id_card: string | undefined;
    // id: number | undefined;
}
const AddDevices = (props: Iprops) => {
    const { addModal, isShowAddModal, actionRef } = props


    // 获取机构下全部患者
    const get_platform_patient = async () => {
        let params = await get_patient_select();
        console.log('全部患者', params);
        let res: any = [];
        params.data.map((item: { username: string; id: string, phone: string, id_card: string }) => {
            let temp: any = {};
            temp['label'] = item.username + '/' + item.phone + '/' + item.id_card;
            temp['value'] = parseInt(item.id);
            res.push(temp);
        });
        return res;
    }
    // 新增绑定设备
    const add_bind_devices = async (values: any) => {
        console.log('value', values);
        let params: any = await add_edit_bioland(values)
        if (params.msg == 'ok') {
            //关闭弹窗
            actionRef.current?.reload();
            message.success('新增成功');
            isShowAddModal(false);
            //刷新列表
        } else {
            message.error(params.msg);
        }
    }
    return (
        <Modal
            title="新增设备"
            open={addModal}
            onCancel={() => isShowAddModal(false)}
            width={500}
            destroyOnClose={true}
            footer={null}
            centered
        >
            <Divider />
            <ProForm<{
                jigou: string;
                reason: string;
            }>
                layout="horizontal"
                labelCol={{ span: 5 }}
                wrapperCol={{ span: 15 }}
                onReset={() => isShowAddModal(false)}
                onFinish={(value) => add_bind_devices(value)}
                submitter={{
                    // 配置按钮文本
                    searchConfig: {
                        resetText: '取消',
                        submitText: '提交',
                    },
                    resetButtonProps: {
                        style: {
                            display: 'block',
                            position: 'absolute',
                            left: 'calc(50% - 107px)',
                        },
                    },
                    submitButtonProps: {
                        style: {
                            display: 'block',
                            position: 'absolute',
                            right: 'calc(50% - 107px)',
                        },
                    },
                }}
            >
                <ProFormSelect
                    name="type"
                    label="设备类型"
                    request={async () => [
                        { label: '血压计', value: 1 },
                        { label: '血糖仪', value: 2 },
                        { label: '血压血糖一体机', value: 3 },
                    ]}
                    placeholder="请选择设备类型"
                    rules={[{ required: true, message: '请选择设备' }]}
                />
                <ProFormText
                    name="sn"
                    label="SN码"
                    placeholder="请输入SN码"
                    rules={[
                        { required: true, message: '请输入SN码' },
                        { pattern: /(^\S)((.)*\S)?(\S*$)/, message: '不能输入空格' },
                        { pattern: /^[0-9A-Za-z]{9}$/, message: '只允许输入9位数字或字母' }
                    ]}
                />
                <ProFormSelect
                    name="patient_id"
                    label="绑定患者"
                    request={async () => get_platform_patient()}
                    placeholder="请选择患者"
                    fieldProps={{
                        showSearch: true,
                        filterOption: (input: any, option: any) =>
                            (option?.label ?? '').toLowerCase().includes(input.toLowerCase())
                    }}
                />
                <br />
            </ProForm>
            <br />
        </Modal>
    )
}
export default AddDevices